const createCachedFunction = function (fn) {
  const map = new Map();

  return function (...args) {
    const key = JSON.stringify(args);
    if (map.has(key)) return map.get(key);

    const res = fn.call(this, ...args);
    map.set(key, res);
    return res;
  };
};

// 模拟一个计算密集型的函数
function expensiveCalculation(a, b) {
  console.log("执行计算...");
  return a + b;
}

const cachedCalc = createCachedFunction(expensiveCalculation);

console.log(cachedCalc(2, 3)); // 输出: "执行计算..." 然后 5
console.log(cachedCalc(2, 3)); // 直接输出: 5 (从缓存中获取)
console.log(cachedCalc(4, 5)); // 输出: "执行计算..." 然后 9
